package org.mock.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.mock.entity.BaifenhuiNotice;
import org.mock.mapper.BaifenhuiNoticeMapper;
import org.mock.request.baiFenHui.DoNoticeRequest;
import org.mock.request.baiFenHui.GetNoticeRequest;
import org.mock.common.ResultBaiFenHui;
import org.mock.response.baiFenHui.BaiFenHuiNoticeVo;
import org.mock.service.BaifenhuiNoticeService;
import org.mock.dto.BaifenhuiNoticeDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mock.common.Condition;
import org.mock.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import cn.hutool.core.util.StrUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author AI
 * @since 2025-05-22
 */
@Service
@AllArgsConstructor
@Slf4j
public class BaifenhuiNoticeServiceImpl implements BaifenhuiNoticeService {

    protected BaifenhuiNoticeMapper baifenhuiNoticeMapper;

    @Override
    public IPage<BaifenhuiNotice> page(BaifenhuiNoticeDTO dto) {
        IPage<BaifenhuiNotice> page = Condition.getPage(dto);
        QueryWrapper<BaifenhuiNotice> queryWrapper = Condition.getQueryWrapper(BeanCopyUtils.copy(dto, BaifenhuiNotice.class));
        if (StrUtil.isNotEmpty(dto.getColumn()) && StrUtil.isNotEmpty(dto.getKeywords())) {
            queryWrapper.like(dto.getColumn(), dto.getKeywords());
        }
        if (StrUtil.isNotEmpty(dto.getOrderAsc())) {
            queryWrapper.orderByAsc(dto.getOrderAsc());
        }
        if (StrUtil.isNotEmpty(dto.getOrderDesc())) {
            queryWrapper.orderByDesc(dto.getOrderDesc());
        }
        if (dto.getStartTime() != null) {
            queryWrapper.gt("created", dto.getStartTime());
        }
        if (dto.getEndTime() != null) {
            queryWrapper.lt("created", dto.getEndTime());
        }
        return baifenhuiNoticeMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<BaifenhuiNotice> list(BaifenhuiNoticeDTO dto) {
        QueryWrapper<BaifenhuiNotice> queryWrapper = Condition.getQueryWrapper(BeanCopyUtils.copy(dto, BaifenhuiNotice.class));
        if (StrUtil.isNotEmpty(dto.getColumn()) && StrUtil.isNotEmpty(dto.getKeywords())) {
            queryWrapper.like(dto.getColumn(), dto.getKeywords());
        }
        if (StrUtil.isNotEmpty(dto.getOrderAsc())) {
            queryWrapper.orderByAsc(dto.getOrderAsc());
        }
        if (StrUtil.isNotEmpty(dto.getOrderDesc())) {
            queryWrapper.orderByDesc(dto.getOrderDesc());
        }
        if (CollectionUtil.isNotEmpty(dto.getTypeList())) {
            queryWrapper.in("type", dto.getTypeList());
        }
        return baifenhuiNoticeMapper.selectList(queryWrapper);
    }

    @Override
    public Integer save(BaifenhuiNoticeDTO dto) {
        return baifenhuiNoticeMapper.insert(BeanCopyUtils.copy(dto, BaifenhuiNotice.class));
    }

    @Override
    public Integer updateById(BaifenhuiNoticeDTO dto) {
        return baifenhuiNoticeMapper.updateById(BeanCopyUtils.copy(dto, BaifenhuiNotice.class));
    }


    @Override
    public Integer delete(BaifenhuiNoticeDTO dto) {
        LambdaQueryWrapper<BaifenhuiNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaifenhuiNotice::getNoticeId, dto.getNoticeId());
        return baifenhuiNoticeMapper.delete(wrapper);
    }

    @Override
    public BaifenhuiNotice getOne(BaifenhuiNoticeDTO dto) {
        return baifenhuiNoticeMapper.selectOne(Condition.getQueryWrapper(BeanCopyUtils.copy(dto, BaifenhuiNotice.class)));
    }

    @Override
    public ResultBaiFenHui getNotice(GetNoticeRequest request) {
        try {
            String type = request.getType();
            String[] split = type.split(",");
            List<String> typeList = Arrays.asList(split);
            BaifenhuiNoticeDTO dto = new BaifenhuiNoticeDTO();
            dto.setTypeList(typeList);
            List<BaifenhuiNotice> list = this.list(dto);

            JSONObject data = new JSONObject();
            data.put("notice_num", list.size());
            //要把里面的json解析
            List<BaiFenHuiNoticeVo> collect = list.stream().map(item -> {
                BaiFenHuiNoticeVo vo = BeanCopyUtils.copy(item, BaiFenHuiNoticeVo.class);
                String result = item.getResult();
                JSONObject jsonObject = JSONObject.parseObject(result);
                vo.setResult(jsonObject);
                //恶心的百分汇，一会儿用下划线，一会儿用驼峰
                vo.setNotice_id(item.getNoticeId());
                return vo;
            }).collect(Collectors.toList());
            data.put("notice_list", collect);
            return ResultBaiFenHui.success(data);
        } catch (Exception e) {
            log.error("异常：{}", e.getMessage(), e);
            return ResultBaiFenHui.error("999", e.getMessage());
        }
    }

    @Override
    public ResultBaiFenHui doNotice(DoNoticeRequest request) {
        try {
            String notice_ids = request.getNotice_ids();
            String[] noticeIds = notice_ids.split(",");
            //删除通知
            int delete = baifenhuiNoticeMapper.delete(new LambdaQueryWrapper<BaifenhuiNotice>()
                    .in(BaifenhuiNotice::getNoticeId, noticeIds)
            );

            return ResultBaiFenHui.success(delete);
        } catch (Exception e) {
            log.error("异常：{}", e.getMessage(), e);
            return ResultBaiFenHui.error("999", e.getMessage());
        }
    }
}
